package com.coolbi.onsupai.admin.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.onsupai.core.biz.SmsBiz;
import com.coolbi.onsupai.core.constant.SmsConstant;
import com.coolbi.onsupai.core.entity.Sms;
import com.coolbi.security.entity.User;
import com.coolbi.security.web.action.SessionAction;

/**
 * @Description SMS Action 短消息Action
 * @author for2cold
 * @codeDate 2012-7-7 上午11:32:16
 *
 */
public class SmsAction extends SessionAction {

	private static final long serialVersionUID = 1L;
	
	private Sms sms;
	private List<Sms> smsList;
	@Autowired
	private SmsBiz smsBiz;
	private Boolean readStatus;
	private	Integer smsType;
	private boolean result;
	
	/**
	 * 发送短消息
	 * @return
	 */
	public String save() {
		try {
			User user = getCurrentUser();
			sms.setAddresserId(user.getId());
			smsBiz.save(sms);
			result = true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			result = false;
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 更改阅读状态为已读
	 * @return
	 */
	public String updateToIsReaded() {
		try {
			smsBiz.updateReadStatus(sms.getId(), SmsConstant.IS_READED);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			return ERROR;
		}
		return NONE;
	}
	
	/**
	 * 根据管理员ID获取接收的消息
	 * @return
	 * @throws Exception
	 */
	public String listReceiveByPage() {
		User user = getCurrentUser();
		try {
			pager.setRecordCount(smsBiz.count(user.getId(), readStatus, smsType));
			smsList = smsBiz.findByPage(user.getId(), readStatus, smsType, (pager.getCurrPageNo() - 1) * pager.getPageSize(), pager.getPageSize());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 根据管理员ID获取已发消息
	 * @return
	 */
	public String listSendByPage() {
		User user = getCurrentUser();
		try {
			pager.setRecordCount(smsBiz.count(user.getId()));
			smsList = smsBiz.findByPage(user.getId(), (pager.getCurrPageNo() - 1) * pager.getPageSize(), pager.getPageSize());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public Sms getSms() {
		return sms;
	}
	public void setSms(Sms sms) {
		this.sms = sms;
	}
	public List<Sms> getSmsList() {
		return smsList;
	}
	public void setSmsList(List<Sms> smsList) {
		this.smsList = smsList;
	}
	public void setSmsBiz(SmsBiz smsBiz) {
		this.smsBiz = smsBiz;
	}

	public Integer getSmsType() {
		return smsType;
	}

	public void setSmsType(Integer smsType) {
		this.smsType = smsType;
	}

	public Boolean getReadStatus() {
		return readStatus;
	}

	public void setReadStatus(Boolean readStatus) {
		this.readStatus = readStatus;
	}

	public boolean getResult() {
		return result;
	}

	public void setResult(boolean result) {
		this.result = result;
	}
}
